package com.rxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.rxt.model.domain.OAddressBook;
import com.rxt.mapper.OAddressBookMapper;
import com.rxt.model.dto.AddressDto;
import com.rxt.model.dto.UUserDto;
import com.rxt.model.vo.AddressVo;
import com.rxt.service.IOAddressBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxt.util.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Service
public class OAddressBookServiceImpl extends ServiceImpl<OAddressBookMapper, OAddressBook> implements IOAddressBookService {

    @Autowired
    private OAddressBookMapper addressBookMapper;

    @Override
    public void add(AddressVo addressVo) {
        UUserDto userDto = BaseContext.get();
        OAddressBook addressBook = new OAddressBook();
        BeanUtils.copyProperties(addressVo,addressBook);
        addressBook.setUserId(userDto.getId());
        if (addressVo.getIsDefault()) {
            LambdaUpdateWrapper<OAddressBook> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(OAddressBook::getUserId,userDto.getId()).set(OAddressBook::getIsDefault,0);
            this.update(wrapper);
        }
            addressBookMapper.insert(addressBook);
    }

    @Override
    public void isDefault(Long addrId) {
        UUserDto userDto = BaseContext.get();
        LambdaUpdateWrapper<OAddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OAddressBook::getUserId,userDto.getId()).set(OAddressBook::getIsDefault,0);
        this.update(wrapper);
        LambdaUpdateWrapper<OAddressBook> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.eq(OAddressBook::getId,addrId).set(OAddressBook::getIsDefault,1);
        this.update(wrapper1);
    }

    @Override
    public void updateAddress(AddressVo addressVo) {
        UUserDto userDto = BaseContext.get();
        if (addressVo.getIsDefault()) {
            LambdaUpdateWrapper<OAddressBook> wrapper1 = new LambdaUpdateWrapper<>();
            wrapper1.eq(OAddressBook::getUserId,userDto.getId()).set(OAddressBook::getIsDefault,0);
            this.update(wrapper1);
        }
        LambdaUpdateWrapper<OAddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OAddressBook::getUserId,userDto.getId())
                .eq(OAddressBook::getId,addressVo.getAddrId())
                .set(OAddressBook::getConsignee,addressVo.getConsignee())
                .set(OAddressBook::getDetail,addressVo.getDetail())
                .set(OAddressBook::getPhone,addressVo.getPhone())
                .set(OAddressBook::getIsDefault,addressVo.getIsDefault());
        this.update(wrapper);
    }

    @Override
    public List<OAddressBook> findAll() {
        UUserDto userDto = BaseContext.get();
        LambdaQueryWrapper<OAddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OAddressBook::getUserId,userDto.getId());
        return addressBookMapper.selectList(wrapper);
    }

    @Override
    public AddressDto setAddr() {
        UUserDto userDto = BaseContext.get();
        LambdaQueryWrapper<OAddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OAddressBook::getUserId,userDto.getId());
        List<OAddressBook> addressBooks = addressBookMapper.selectList(wrapper);
        if (addressBooks.size() == 0) {
            AddressDto addressDto = new AddressDto();
            addressDto.setFlag(0);
            return addressDto;
        }else {
            for (OAddressBook addressBook : addressBooks) {
                if (addressBook.getIsDefault()) {
                    AddressDto addressDto = new AddressDto();
                    BeanUtils.copyProperties(addressBook,addressDto);
                    addressDto.setFlag(1);
                    return addressDto;
                }
            }
        }
        OAddressBook addressBook = addressBooks.get(addressBooks.size()-1);
        AddressDto addressDto = new AddressDto();
        BeanUtils.copyProperties(addressBook,addressDto);
        addressDto.setFlag(2);
        return addressDto;
    }
}
